package com.yk.faceapplication.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.HttpDownloader;
import com.yk.faceapplication.util.LogDebugger;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by yk_2018/1/17_17:40
 */

public class AgreeWebviewActivity extends BaseActivity{
    private WebView webView;
    private String url;
    private String fileData;
    private final int SHOW_WEBVIEW = 1000;
    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    private void getUrl() {


        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                try {
                    url = jsonObject.getString("data");
                    new downloadFileThread().start();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.getRequest(this, RequestUrl.GET_REGISTER_URL, callback);
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        webView = (WebView) findViewById(R.id.m_webview);
        // 开启 localStorage
        webView.getSettings().setDomStorageEnabled(true);
//        webView.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        // 设置支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 启动缓存
        webView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        //使用自定义的WebViewClient
//        webView.setWebViewClient(new WebViewClient()
//        {
//            //覆盖shouldOverrideUrlLoading 方法
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url)
//            {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        webView.loadUrl(getIntent().getStringExtra("url"));
        getUrl();
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        if(msg.what == SHOW_WEBVIEW){
            webView.loadData(fileData,"text/html; charset=UTF-8", null);
        }
    }

    class downloadFileThread extends Thread{
        public void run(){
            HttpDownloader httpDownloader=new HttpDownloader();
            fileData=httpDownloader.downloadFiles(url);
            sendMessage(SHOW_WEBVIEW);
        }
    }


    // 覆盖onKeydown 添加处理WebView 界面内返回事件处理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack())
        {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
